//
//  SearchTypeView.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/6/5.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "SearchTypeView.h"
#import <Masonry/Masonry.h>

@interface SearchTypeView()

@property (nonatomic,strong) UIButton * baobeiBtn;
@property (nonatomic,strong) UIButton * shopBtn;

@end

@implementation SearchTypeView

- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        _baobeiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_baobeiBtn setTitle:@"宝贝" forState:UIControlStateNormal];
        [_baobeiBtn setTitleColor:[UIColor redF2color] forState:UIControlStateSelected];
        _baobeiBtn.tag = 0;
        _baobeiBtn.selected = YES;
        [_baobeiBtn setTitleColor:[UIColor grayTextColor] forState:UIControlStateNormal];
        [_baobeiBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _shopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shopBtn setTitleColor:[UIColor redF2color] forState:UIControlStateSelected];
        [_shopBtn setTitleColor:[UIColor grayTextColor] forState:UIControlStateNormal];
        [_shopBtn setTitle:@"店铺" forState:UIControlStateNormal];
        _shopBtn.tag = 1;
        [_shopBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.baobeiBtn];
        [self addSubview:self.shopBtn];
        [self _layout];
    }
    return self;
}

- (void)_layout{
    [self.baobeiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.right.equalTo(self.mas_centerX);
    }];
    [self.shopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.left.equalTo(self.mas_centerX);
    }];
    UIView * line = [UIView new];
    line.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(-5);
        make.centerX.equalTo(self);
        make.width.equalTo(@.5);
    }];
    UIView * btmLine = [UIView new];
//    btmLine.backgroundColor
    btmLine.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5];
    [self addSubview:btmLine];
    [btmLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.equalTo(@.5);
    }];
}

- (void)btnClicked:(UIButton *)btn{
    if (btn.selected) {
        return;
    }
    self.baobeiBtn.selected = NO;
    self.shopBtn.selected = NO;
    btn.selected = YES;
    
    if (self.didClickedBtnBlock) {
        self.didClickedBtnBlock(btn.tag);
    }
}

- (void)setSelectedIndex:(NSInteger)index{
    if (index == 0) {
        [self btnClicked:self.baobeiBtn];
    }else{
        [self btnClicked:self.shopBtn];
    }
}
@end

